<template>
  <div class="login-page">
    <div class="login-header">
      <h1 class="site-logo">Petster</h1>
      <p>您的宠物健康生活指南</p>
    </div>
    
    <div class="login-container">
      <div class="login-box">
        <h2>用户登录</h2>
        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-group">
            <label for="username">用户名</label>
            <input
              id="username"
              v-model="username"
              type="text"
              placeholder="请输入用户名"
              required
              class="form-input"
            />
          </div>
          <div class="form-group">
            <label for="password">密码</label>
            <input
              id="password"
              v-model="password"
              type="password"
              placeholder="请输入密码"
              required
              class="form-input"
            />
          </div>
          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" class="checkbox" v-model="rememberMe" />
              <span>记住我</span>
            </label>
            <a href="#" class="forgot-password" @click.prevent="handleForgotPassword">忘记密码？</a>
          </div>
          <button type="submit" class="submit-button" :disabled="isLoading">
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
          <div v-if="error" class="error-message">{{ error }}</div>
          
          <div class="register-link">
            <p>还没有账号？ <router-link to="/register">立即注册</router-link></p>
          </div>
        </form>
      </div>
    </div>
    
    <div class="login-footer">
      <p>宠物爱好者社区 &copy; {{ new Date().getFullYear() }}</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useAuth } from '../composables/useAuth';

const router = useRouter();
const username = ref('');
const password = ref('');
const rememberMe = ref(false);
const error = ref('');
const isLoading = ref(false);
const { login } = useAuth();

// 处理登录逻辑
const handleLogin = async () => {
  // 重置错误信息
  error.value = '';
  
  try {
    isLoading.value = true;
    // 使用 useAuth 组合式函数进行登录
    await login(username.value, password.value, rememberMe.value);
    // 登录成功后跳转到个人主页
    router.push('/profile');
  } catch (err) {
    error.value = err.message || '登录失败，请重试';
  } finally {
    isLoading.value = false;
  }
};

// 处理忘记密码
const handleForgotPassword = () => {
  // 这里可以实现忘记密码的逻辑
  alert('忘记密码功能即将上线，敬请期待！');
};
</script>

<style scoped>
/* 全局样式变量 */
:root {
  --primary-color: #4CAF50;
  --primary-hover: #3d8b40;
  --text-primary: #333;
  --text-secondary: #666;
  --white: #ffffff;
  --light-gray: #f5f5f5;
  --border-color: #e0e0e0;
  --error-color: #f44336;
  --radius: 12px;
  --transition: all 0.3s ease;
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

/* 登录页面整体布局 */
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: var(--light-gray);
}

/* 登录页面头部 */
.login-header {
  text-align: center;
  padding: 30px 20px;
  background-color: var(--white);
  border-bottom: 1px solid var(--border-color);
}

.login-header .site-logo {
  font-size: 2.5rem;
  font-weight: bold;
  color: var(--primary-color);
  margin: 0 0 10px 0;
}

.login-header p {
  color: var(--text-secondary);
  font-size: 1.1rem;
  margin: 0;
}

/* 登录容器 */
.login-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 20px;
}

/* 登录框 */
.login-box {
  width: 100%;
  max-width: 450px;
  background: var(--white);
  padding: 40px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border-color);
}

.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: var(--text-primary);
  font-size: 1.8rem;
}

/* 登录表单 */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 表单组 */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  color: var(--text-primary);
  font-weight: 500;
  font-size: 14px;
}

/* 表单输入框 */
.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 16px;
  transition: var(--transition);
  background-color: var(--white);
}

.form-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
}

.form-input::placeholder {
  color: #aaa;
  font-size: 14px;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
}

.checkbox {
  width: auto;
  margin: 0;
  cursor: pointer;
}

.forgot-password {
  color: var(--primary-color);
  text-decoration: none;
  font-size: 14px;
  transition: var(--transition);
}

.forgot-password:hover {
  color: var(--primary-hover);
  text-decoration: underline;
}

/* 提交按钮 */
.submit-button {
  width: 100%;
  padding: 14px;
  background-color: var(--primary-color);
  color: var(--white);
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
}

.submit-button:hover:not(:disabled) {
  background-color: var(--primary-hover);
  transform: translateY(-2px);
}

.submit-button:active:not(:disabled) {
  transform: scale(0.98);
}

.submit-button:disabled {
  background-color: var(--border-color);
  cursor: not-allowed;
  transform: none;
}

/* 错误信息 */
.error-message {
  color: var(--error-color);
  padding: 10px;
  background-color: #fee;
  border-radius: var(--radius);
  font-size: 14px;
  text-align: center;
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: var(--text-secondary);
}

.register-link a {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 500;
  transition: var(--transition);
}

.register-link a:hover {
  color: var(--primary-hover);
  text-decoration: underline;
}

/* 登录页脚 */
.login-footer {
  text-align: center;
  padding: 20px;
  background-color: var(--white);
  border-top: 1px solid var(--border-color);
  color: var(--text-secondary);
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 576px) {
  .login-header {
    padding: 20px;
  }
  
  .login-header .site-logo {
    font-size: 2rem;
  }
  
  .login-container {
    padding: 20px;
  }
  
  .login-box {
    padding: 30px 20px;
  }
  
  .login-box h2 {
    font-size: 1.5rem;
  }
  
  .form-input {
    padding: 10px 12px;
    font-size: 14px;
  }
  
  .submit-button {
    padding: 12px;
    font-size: 15px;
  }
}
</style>